package com.qf.lanzhou.shop.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qf.lanzhou.shop.api.exceptions.BaseExceptions;
import com.qf.lanzhou.shop.pojo.entity.Goods;
import com.qf.lanzhou.shop.pojo.entity.ShopCart;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import com.qf.lanzhou.shop.api.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 是柠檬甜
 * @date 2021/1/8 16:08
 */
@Api(tags = "购物车")
@Controller
public class ShopCartController extends BaseController {
    @Autowired
    private ShopCartService shopCartService;

    @ApiOperation("展示购物车商品")
    @RequestMapping("/shopCartShow")
    @ResponseBody
    public BaseRespVO shopCartShow(ShopCart shopCart, HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        Long userId = user.getId();
        shopCart.setUserId(userId);
        List<ShopCart> list = shopCartService.selectAll(shopCart);
        if (list.size() == 0) {
            return convertSuccessResult(0);
        } else {
            return convertSuccessResult(list);
        }
    }

    @ApiOperation("删除购物车商品")
    @RequestMapping("/delete")
    @ResponseBody
    public BaseRespVO delete(ShopCart shopCart) {
        String message = shopCartService.updateStatus(shopCart);
        return convertSuccessResult(message);
    }

    @ApiOperation("修改购物车商品数量")
    @RequestMapping("/addOrReduceNum")
    @ResponseBody
    public BaseRespVO addOrReduceNum(ShopCart shopCart,HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        Long userId = user.getId();
        shopCart.setUserId(userId);
        System.out.println(shopCart);
        shopCartService.updateGoodsNum(shopCart);
        return convertSuccessResult("执行成功");
    }

    @ApiOperation("加入购物车")
    @RequestMapping("/addGoods")
    @ResponseBody
    public BaseRespVO addGoods(ShopCart shopCart,HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            throw new BaseExceptions(300,"未登录，请先登录");
        }
        Long userId = user.getId();
        shopCart.setUserId(userId);
        System.out.println(shopCart);
        shopCartService.addGoods(shopCart);
        return convertSuccessResult("成功加入购物车！");
    }

    @ApiOperation("未登录异常")
    @RequestMapping("/unLogin")
    @ResponseBody
    public BaseRespVO unLogin(HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            throw new BaseExceptions(300,"未登录，请先登录");
        }
        return convertSuccessResult("已登录");
    }
}
